#include "TrackEagle.h"
#include "Eagle.h"
TrackEagle::TrackEagle(Eagle* _obj, std::string _name, int _id)
	:TrackerObject(_obj,_name,_id)
{
	mEagleObject = _obj;
	mEagleInitPos = _obj->GetPosition();
}
TrackEagle::~TrackEagle()
{
	if (mEagleObject)
	{
		delete mEagleObject;
		mEagleObject = 0;
	}
}
void TrackEagle::Render()
{
	mEagleObject->Render();
}
void TrackEagle::Update(float _elapsedTime,KinectWinMgr *mgr)
{
	mEagleObject->Update(_elapsedTime);
	UpdateTackedObj(mgr);
}
void TrackEagle::UpdateSkeletonInfo(Vector4 src[20])
{
	for (int i=0;i<20;i++)
	{
		mEagleSkeletonPos[i]=src[i];
	}
}
void TrackEagle::UpdateTackedObj(KinectWinMgr *kinMgr)
{
	if(GetSkeletonID() == -1)
	{
		mEagleObject->SetPosition(mEagleInitPos);
		return;
	}

	if (GetTrackEagleState() == EagleNormalState)
	{		
		float rightHandY = kinMgr->SkeletonToScreen(mEagleSkeletonPos[NUI_SKELETON_POSITION_HAND_RIGHT]).y;
		float shoulderCenterY = kinMgr->SkeletonToScreen(mEagleSkeletonPos[NUI_SKELETON_POSITION_SHOULDER_CENTER]).y;

		float offsetY = shoulderCenterY - rightHandY;

		QVector3D pos = mEagleInitPos;
		float factor = 2.5f;
		QVector3D eaglePos = QVector3D(pos.x(), pos.y() + offsetY * factor, pos.z());

		if(eaglePos.y() > 700.0f)
			eaglePos.setY(700.f);
		else if(eaglePos.y() < 50.f)
			eaglePos.setY(50.f);
		mEagleObject->SetPosition(eaglePos);
	}
	else if(GetTrackEagleState() == EagleGetBirdState)
	{		
		//in this state the eagle is no leonger move
		static  int iCount =0;
		if (iCount++>60)
		{
			SetTrackEagleState(EagleNormalState);
			iCount = 0;
		}
	}
}